<?php
function file_size($size)
{
	if (empty($size))
		return '0 B';
        if ($size > 1024*1024*1024)
                return round($size / 1024 / 1024 / 1024, 2) . ' Gb';
        if ($size > 1024*1024)
                return round($size / 1024 / 1024, 2) . ' Mb';
        if ($size > 1024)
                return round($size / 1024, 2) . ' Kb';
        return $size . ' B';
}

$sql = '
SELECT *
FROM projects
LEFT JOIN tasks ON task_project = project_id';
if (!empty($project_id))
	$sql .= ' WHERE project_id = ' . $project_id;
$all_tasks = db_loadList($sql);

$sql = '
SELECT *, round(sum(task_log_hours),2) as work
FROM projects
LEFT JOIN tasks ON task_project = project_id
LEFT JOIN user_tasks ON user_tasks.task_id = tasks.task_id
LEFT JOIN users ON user_tasks.user_id = users.user_id
LEFT JOIN task_log ON task_log_task = tasks.task_id AND task_log_creator = users.user_id';
if (!empty($project_id))
	$sql .= ' WHERE project_id = ' . $project_id;
$sql .= ' GROUP BY tasks.task_id, users.user_id';
$users_all = db_loadList($sql);

foreach ($users_all as $user) {
	$uid = $user['user_id'];
	if (!isset($uid) || $uid <= 0) continue;
	$users_per_task[$user['task_id']][] = $uid;
	$users[$uid]['all'][$user['task_id']] = $user;
	$users[$uid]['name'] = !empty($user['user_username']) ? $user['user_username']: $uid;
	$users[$uid]['hours'] = 0;
	$users[$uid]['completed'] = array();
	$users[$uid]['inprogress'] = array();
	$users[$uid]['pending'] = array();
	$users[$uid]['overdue'] = array();
}

$tasks['hours'] = 0;
$tasks['inprogress'] = array();
$tasks['completed'] = array();
$tasks['pending'] = array();
$tasks['overdue'] = array();
foreach($all_tasks as $task) {
	if ($task['task_percent_complete'] == 100)
		$tasks['completed'][] = & $task;
	else {
		if ($task['task_end_date'] < date('Y-m-d'))
			$tasks['overdue'][] = & $task;
		if ($task['task_percent_complete'] == 0)
			$tasks['pending'][] = & $task;
		else
			$tasks['inprogress'][] = & $task;
	}

	if (isset($users_per_task[$task['task_id']])) {
		foreach ($users_per_task[$task['task_id']] as $user) {
			if ($task['task_percent_complete'] == 100)
				$users[$user]['completed'][] = & $task;
			else {
				if ($task['task_end_date'] < date('Y-m-d'))
					$users[$user]['overdue'][] = & $task;
				if ($task['task_percent_complete'] == 0)
					$users[$user]['pending'][] = & $task;
				else
					$users[$user]['inprogress'][] = & $task;
			}

			$hrs = $users[$user]['all'][$task['task_id']]['work'];
			$users[$user]['hours'] += $hrs;
			$tasks['hours'] += $hrs;
		}
	}
}

$sql = '
SELECT sum(file_size)
FROM files
WHERE file_project = ' . $project_id . '
GROUP BY file_project';
$files = db_loadResult($sql);

$ztasks = count($all_tasks);
$zotasks = count($tasks['overdue']);
$zctasks = count($tasks['completed']);
$zitasks = count($tasks['inprogress']);
$zptasks = count($tasks['pending']);
$ontime = round(100 * (1 - ($zotasks / $ztasks) - ($zctasks / $ztasks)));
?>

<table width="100%" border="0" cellpadding="4" cellspacing="0" class="subinfopanel">
<tr>
	<td width="100%" bgcolor="white" align="center">
		<?php
			$data = round($zctasks * 100 / $ztasks) . ',' .
					round($zitasks * 100/ $ztasks) . ',' .
					round($zptasks * 100 / $ztasks);
			$src = "chart.php?data=$data&legend=completed,inprogress,pending&".
					"colors=springgreen,aquamarine,gold&title=Progress_Chart";
			echo '<img src="'.$src.'">';
			$data = round($zctasks * 100 / $ztasks).','.$ontime.','.
					round($zotasks * 100 / $ztasks);
			$src = "chart.php?data=$data&legend=completed,ontime,overdue&".
					"colors=springgreen,aquamarine,tomato&title=Time_Chart";
			echo '<img src="'.$src.'">';
		?>
	</td>
</tr>
</table>
<br />

<table width="100%" cellspacing="0" cellpadding="4" border="0">
<tr>
	<th class="last" colspan="2"><?=$AppUI->_('Current Project Status')?></th>
</tr>
<tr>
	<td colspan="2">
		<table cellspacing="4" cellpadding="0" border="0">
		<tr>
			<td><b><?php echo $AppUI->_('Team size'); ?>:</b></td>
			<td>&nbsp;<?php echo count($users); ?> <?php echo $AppUI->_('members'); ?></td>
			<td>&nbsp;&nbsp;<b><?php echo $AppUI->_('Space utilized'); ?>:</b></td>
			<td nowrap="nowrap">&nbsp;<?php echo file_size($files); ?></td>
		</tr>
		</table>
	</td>
</tr>
<tr style="background: white">
	<td valign="top">
		<table width="100%" cellspacing="0" cellpadding="4" border="0" class="list">
		<tr>
			<th><?=$AppUI->_('Status')?></th>
			<th><?=$AppUI->_('Details')?></th>
			<th class="last" align="center">%</th>
		</tr>
		<tr class="second">
			<td nowrap><?=$AppUI->_('Complete')?></td>
			<td align="center"><?=$zctasks?></td>
			<td align="right"><?=round($zctasks * 100 / $ztasks)?>%</td>
		</tr>
		<tr class="second">
			<td nowrap><?=$AppUI->_('In Progress')?></td>
			<td align="center"><?=$zitasks; ?></td>
			<td align="right"><?=round($zitasks * 100 / $ztasks)?>%</td>
		</tr>
		<tr class="second">
			<td nowrap><?=$AppUI->_('Not Started')?></td>
			<td align="center"><?=$zptasks?></td>
			<td align="right"><?=round($zptasks * 100 / $ztasks)?>%</td>
		</tr>
		<tr class="second">
			<td nowrap><?=$AppUI->_('Past Due')?></td>
			<td align="center"><?=$zotasks?></td>
			<td align="right"><?=round($zotasks * 100 / $ztasks)?>%</td>
		</tr>
		<tr>
			<td><?=$AppUI->_('Total')?></td>
			<td align="center"><?=$ztasks?></td>
			<td align="right">100%</td>
		</tr>
		</table>
	</td>
	<td width="100%" valign="top">
		<table width="100%" cellspacing="0" cellpadding="4" border="0" class="list">
		<tr>
			<th><?=$AppUI->_('Task per Assignee')?></th>
			<th><?=$AppUI->_('Pending')?></th>
			<th><?=$AppUI->_('Overdue')?></th>
			<th><?=$AppUI->_('In progress')?></th>
			<th><?=$AppUI->_('Completed')?></th>
			<th><?=$AppUI->_('Total')?></th>
			<th class="last"><?=$AppUI->_('Hours worked')?></th>
		</tr>
		<?php foreach($users as $user => $stats) {?>
		<tr class="second">
			<td><?=$stats['name']?></td>
			<td align="center"><?=count($stats['pending'])?></td>
			<td align="center"><?=count($stats['overdue'])?></td>
			<td align="center"><?=count($stats['inprogress'])?></td>
			<td align="center"><?=count($stats['completed'])?></td>
			<td align="center"><?=count($stats['all'])?></td>
			<td align="right"><?=$stats['hours']?> <?=$AppUI->_('hours')?></td>
		</tr>
		<?php } ?>
		<tr style="font-weight: bold">
			<td align="right"><?=$AppUI->_('Real Total')?></td>
			<td align="center"><?=$zptasks?></td>
			<td align="center"><?=$zotasks?></td>
			<td align="center"><?=$zitasks?></td>
			<td align="center"><?=$zctasks?></td>
			<td align="center"><?=$ztasks?></td>
			<td align="right"><?=$tasks['hours']?> <?=$AppUI->_('hours')?></td>
		</tr>
		</table>
	</td>
</tr>
</table>
